package com.yuyou.hxyy.service.api.bean;



import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;



public class ReturnResult<T> {

    private boolean success;
    private String desc;
    private T data;
    private String code;
    private String codeMess;
  
    public ReturnResult(boolean success, String desc , T data,String code,String codeMess) {
        this.success = success;
        this.desc = desc;
        this.data = data;
        this.code=code;
        this.codeMess=codeMess;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    public String getCodeMess() {
		return codeMess;
	}

	public void setCodeMess(String codeMess) {
		this.codeMess = codeMess;
	}

	public static ReturnResult success() {
        return new ReturnResult(true, null,ResBoolSimpleInfo.getSuccessInfo(),ReturnCode.code._1001.getCode(),ReturnCode.code._1001.getMessage());
    }

    public static ReturnResult success(String desc) {
    	return new ReturnResult(true, desc,null,ReturnCode.code._1001.getCode(),ReturnCode.code._1001.getMessage());
    }

    public static <T> ReturnResult success(Pagination<T> page) {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("totalPage", page.getTotalPage());
        data.put("pageIndex", page.getPageIndex());
        data.put("total", page.getTotal());
        data.put("list", page.getList());
        data.put("ext", page.getExt());
        return new ReturnResult(true, null,data,ReturnCode.code._1001.getCode(),ReturnCode.code._1001.getMessage() );
    }

    public static <T> ReturnResult returnRes(boolean success,String desc,T data) {
        return new ReturnResult(success, desc,data,ReturnCode.code._1001.getCode(),ReturnCode.code._1001.getMessage());
    }
    
    public static <T> ReturnResult returnRes(ResBoolSimpleInfo<T> res) {
        return new ReturnResult(res.isSuccess(), res.getMessage(),res.getData(),ReturnCode.code._1001.getCode(),ReturnCode.code._1001.getMessage());
    }
    
    public static <T> ReturnResult success(T data) {
        return new ReturnResult(true, null,data,ReturnCode.code._1001.getCode(),ReturnCode.code._1001.getMessage() );
    }

    public static<T> ReturnResult success(String desc,T data,String code,String mess ) {
    	return new ReturnResult(true, desc,data,code,mess);
    }
    
    public static ReturnResult fail(String desc ) {
    	return new ReturnResult(false, desc,null,ReturnCode.code._2001.getCode(),ReturnCode.code._2001.getMessage());
    }
    public static<T> ReturnResult fail(String desc,T data,String code,String mess ) {
    	return new ReturnResult(false, desc,data,code,mess);
    }

    public static ReturnResult fail(String desc , Object data ) {
    	return new ReturnResult(false, desc,data,ReturnCode.code._2001.getCode(),ReturnCode.code._2001.getMessage());
    }


    public String  toJsonString() {
        return JSONObject.toJSONString(this,true);
    }
    
    public HashMap<String,Object>  toMap() {
    	HashMap<String,Object> map = 
    			JSON.parseObject(toJsonString(),new TypeReference<HashMap<String,Object>>() {});
    	return map;
    }

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
    
    
    
    
}
